#include "mapvalue.h"
#include <QDebug>
#include <QDataStream>
#include <QFile>
static const QString headStr="mapvalues" ;
MapValue::MapValue(QObject *parent) : QObject(parent)
{
    m_size=64;
    for(int i=0;i<m_size;i++)
    {
        d.append(false);
    }
}

bool MapValue::value(const int &index)
{
    return d[index];
}

void MapValue::setValue(const int &index, const bool &value)
{
    d[index]=value;
}

int MapValue::size()
{
    return m_size;
}

bool& MapValue::operator [](const int &index)
{
    return d[index];
}

void MapValue::printValue()
{
    qDebug()<<d;
}

void MapValue::loadValues(const QString &fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::ReadOnly))
    {
        qWarning()<<"open file "<<fileName<<"failed!";
        file.close();
        return;
    }
    QDataStream in(&file);
    in.setVersion(QDataStream::Qt_5_7);
    QString head;
    in>>head;
    if(head!=headStr)
    {
        qWarning()<<"invalid file "<<fileName<<"failed!";
        file.close();
        return;
    }
    in>>d;
    file.close();
}

void MapValue::saveValues(const QString &fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::WriteOnly))
    {
        qWarning()<<"open file "<<fileName<<"failed!";
        file.close();
        return;
    }
    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_5_7);
    //文件头
    out<<headStr;
    //数组大小
    out<<d;
    file.close();
}
